package com.citywithincity.api.startup;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.citywithincity.api.ApiConfig;
import com.citywithincity.api.StartupPolicy;
import com.citywithincity.db.EntityBuilder;
import com.damai.action.Actions;
import com.damai.action.IAction;
import com.damai.action.aop.Interceptor;
import com.damai.plugins.IPlugin;

import javassist.ClassPool;

public class ManualLoadPolicy implements StartupPolicy {
	private List<IPlugin> plugins = new ArrayList<IPlugin>();
	List<Interceptor> interceptors = new ArrayList<Interceptor>();
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void startup(ApiConfig module, Map<String, IAction> routeMap) throws Exception {
		module.registerPlugins(plugins);
		
		
		module.registerIntercepters(interceptors);
		
		for (IPlugin iPlugin : plugins) {
			iPlugin.start();
		}
		List<Class<?>> entities = new ArrayList<Class<?>>();
		module.registerEntities(entities);
		
		for (Class<?> entityClass : entities) {
			EntityBuilder.bindEntity(entityClass);
		}
		
		Actions actions = new Actions(ClassPool.getDefault(),routeMap, interceptors);
		module.beforeBindControllers();
		module.registerActions(actions);
		
		
		
		module.beforeStart();
	}

	@Override
	public List<Interceptor> getInterceptors() {
		return interceptors;
	}

}
